package cn.jiyun.user.controller;

import cn.jiyun.user.pojo.Users;
import cn.jiyun.user.service.UserService;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

@GetMapping("/{page}")
    public String toPage(@PathVariable("page") String page){
return page;
}

    @RequestMapping("/addUser")
    public String addUser(Users users) {
       userService.saveUser(users);
        return "ok";
    }
    @RequestMapping("/updateUser")
    public String updateUser(Users users,Model model) {
        userService.updateUser(users);
        List<Users> list = userService.findAll();
        model.addAttribute("list",list);
        return "list";
    }

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<Users> list = userService.findAll();
        model.addAttribute("list",list);
        return "list";
    }



    @RequestMapping("/delete")
    public String delete(String id,Model model) {
userService.delete(id);
        List<Users> list = userService.findAll();
        model.addAttribute("list",list);
        return "list";
    }

    @RequestMapping("/findbyid")
    public String findbyid(String id,Model model) {
       Users users= userService.findbyid(id);

        model.addAttribute("users",users);
      return "users";
    }


    @PostMapping("login")
    public String login(String nam,String pwd,Model model){
Users users=userService.login(nam,pwd);
model.addAttribute("users",users);
return "ok";
    }
}
